# -*- coding: utf-8 -*-

from PyQt4.QtCore import (QThread, QMutex, QMutexLocker, QWriteLocker, SIGNAL)

class CoverSeeker(QThread):
    def __init__(self, model, lock, bookList, parent=None):
        super(CoverSeeker, self).__init__(parent)
        self.model = model
        self.lock = lock
        self.bookList = bookList
        self.stopped = False
        self.mutex = QMutex()
        self.completed = False
        self.bookTotal = len(self.bookList)

    def stop(self):
        with QMutexLocker(self.mutex):
            self.stopped = True

    def isStopped(self):
        with QMutexLocker(self.mutex):
            return self.stopped

    def run(self):
        i = 1
        for book in self.bookList:
            if self.isStopped():
                return
            self.model.get_cover(book)
            self.emit(SIGNAL("downloadCover(QString, QString)"),
                str(i), str(self.bookTotal))
            i += 1


